<?php

use App\Jobs\Fun;
use App\admin\Service\OA;

 ///OA::Json(1, '参数错误');
if (Fun::isAjax()) {

    $TEXT = '';
//execute
    $db_name = $_POST['table'] ?? null;
    if (!$db_name) {
        OA::Json(1, '参数错误');
    }

    $prefix = config('database.connections.mysql.prefix');
    $sql = 'SHOW FULL COLUMNS FROM ' .$prefix. $db_name . ';';
    // DB::update('set names utf8;');//query

    try {
        $list = DB::select($sql);
      //  print_r($list);exit;

    } catch (\Exception $e) {
        OA::Json(1, '查询错误,可能没找到该表:{'.$prefix. $db_name.'},不需要加上前缀');
    }

    $TEXT .= "<?php
 /**
 * Created by 宋帅的自动工具
 * 生成时间:" . Fun::date(time()) . "
 */
  namespace App\Models\Admin;\r\n ";
    $TEXT .= "/** \r\n This is the model class for table \"" . $db_name . "\".\r\n";
///$TEXT .= "//use think\Model;\r\n/**\r\n";
    foreach ($list as $item) {  ///输出成员定义

        $TEXT .= ' * @property ' . str_replace([
                ' ', '(', ')',','
            ], '', $item->Type) . ' ' . $item->Field;
        $TEXT.="\t".$item->Comment;
        $TEXT .= "\r\n";
    }
    $TEXT .= "*/\r\n";
    $className = ucfirst($db_name) . 'Model';
    $TEXT .= "class $className extends BaseModel{\r\n";
    $TEXT .= "    // 当前模型名称\r\n\tprotected \$table = '$db_name';\r\n";

    $TEXT .= "\tpublic function attributeLabels() {\r\n";
    $TEXT .= "\t\treturn [\r\n";
    foreach ($list as $item) {  ///输出成员定义
        $TEXT .= "\t\t\t'" . $item->Field . "' => '" . $item->Comment . "',";
        $TEXT .= "\r\n";
    }
    $TEXT .= "\t\t];\r\n\t}";

    $TEXT .= "\r\n}";


    OA::Json(0, $TEXT);


}


?>

@extends('admin.main')
@section('content')
    <div class="admin-content">
        <div class="admin-content-body">
            <div class="am-cf am-padding">
                <div class="am-fl am-cf">
                    <strong class="am-text-primary am-text-lg">后台设置</strong>
                    /
                    <strong class="am-text-primary am-text-lg">注释生成</strong>
                    {{--/ <small>后台模块一览</small>--}}
                </div>
            </div>

            <div class="am-cf am-padding" style="color:#808080;">
                <div><span>输入表名:</span>
                    <input type="text" value="" id="table"/>
                </div>
                <textarea class="annotation"></textarea>
                <div class="buttonArray">
                    <button class="btn shengcheng">生成</button>
                </div>
            </div>

        </div>
        <style>
            .annotation {
                width: 800px;
                height: 500px;
                background-color: #1f1f1f;
                color: #efefef;
                font-size: 19px;
                margin-bottom: 15px;
                margin-top: 15px;
            }
        </style>
        <script>
            $(function () {
                $('.shengcheng').click(function () {
                    $('.annotation').val("加载中...");
                    MC.api('/mcadmin/system/annotation',
                        {table: $('#table').val()},
                        function (json) {
                             $('.annotation').val(json);
                        },function(error){
                            $('.annotation').val("分析错误\r\n"+error);
                            MC.msg.alert(error);
                        });
                });

            });

        </script>
    </div>
@stop